package repository

import (
	"boy-go/modules/system/model"
	"boy-go/pkg/db"
	"gorm.io/gorm"
)

type SysUserRoleRepository struct {
	DB *gorm.DB
}

func NewSysUserRoleRepository() *SysUserRoleRepository {
	return &SysUserRoleRepository{
		DB: db.GetMasterDB(),
	}
}

func (r *SysUserRoleRepository) Save(userRole *model.SysUserRole) error {
	return r.DB.Model(&model.SysUserRole{}).Create(userRole).Error
}

func (r *SysUserRoleRepository) CountUserRoleByRoleId(roleId int64) int64 {
	var count int64
	err := r.DB.Model(&model.SysUserRole{}).Where("role_id = ?", roleId).Count(&count).Error
	if err == nil {
		return count
	}
	return 0
}
func (r *SysUserRoleRepository) DeleteUserRoleInfo(userId int64, roleId int64) error {
	return r.DB.Model(&model.SysUserRole{}).Where("user_id = ? and role_id = ?", userId, roleId).Delete(&model.SysUserRole{}).Error
}
func (r *SysUserRoleRepository) DeleteUserRoleInfos(roleId int64, userIds []*int64) error {
	return r.DB.Model(&model.SysUserRole{}).Where("role_id = ? and user_id in ?", roleId, userIds).Delete(&model.SysUserRole{}).Error
}
